package Servelts;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.DAO;
import daoImpls.UserDAOImpl;

import java.sql.*;

import vo.User;


public class UserInfoServlet extends HttpServlet {
		
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		    throws ServletException, IOException {
	        doPost(request,response);
		   }
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			String method = request.getParameter("method");// 获取jsp界面的method参数来判断执行什么方法
			if (method.equals("update")){
				update(request, response);// 修改个人信息方法
			}else if(method.equals("logout")){
				logout(request,response); //注销
				
			}
		}
		
		protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
			HttpSession session=request.getSession();;
			PrintWriter out=response.getWriter();
			System.out.println("update");
			
			String uid = request.getParameter("uid");
			String npassword=request.getParameter("pwd");
			String sex=request.getParameter("sex");
			String email=request.getParameter("email");
			String relname=request.getParameter("relname");
			String address=request.getParameter("address");
			String tel=request.getParameter("utel");
			String balance=request.getParameter("balance");
			if(sex.equals("男")|sex.equals("女")){
				try {
					UserDAOImpl<User> userdao = new UserDAOImpl();
					User user = userdao.get(uid);
					System.out.println(user);
					if(user!=null){
						user.setRelName(relname);
						user.setSex(sex);
						user.setEmail(email);
						user.setAddress(address);
						user.setTelephone(tel);
						user.setPwd(npassword);
						user.setBalance(Double.parseDouble(balance));
						System.out.println("1:" + user);
						userdao.update(user);
					}
					
					out.println("<script type='text/javascript'>  alert('修改成功！！'); location.href='listUser'; </script>");
				}catch (Exception e) {
		            System.out.println("找不到驱动类:"+e);
				}
			}else{
				System.out.println("修改失败");
				out.println("<script type='text/javascript'>  alert('修改失败'); location.href='listUser'; </script>");
			}
		}
	

		protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
			HttpSession session=request.getSession();;
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			System.out.println(session.getAttribute("username") + "注销...");
			session.invalidate();
			out.println("<script type='text/javascript'>  alert('注销成功，为您跳转到登录界面'); location.href='Login_Register.jsp'; </script>");
			
		}
}

